package com.nicolasprof.profgallery.controls {
	import flash.display.InteractiveObject;
	import flash.events.MouseEvent;

	/**
	 * @author Nicolas Prof
	 */
	public class UIComponent extends Object {

		public static const TEXTFIELD_CURRENT : String = "tfCurrent";
		public static const TEXTFIELD_ALL : String = "tfAll";
		public static const BUTTON_PREVIOUS : String = "btnPrevious";
		public static const BUTTON_NEXT : String = "btnNext";
		public static const BUTTON_SLIDESHOW : String = "btnSlideshow";
		public static const BUTTON_CATEGORIES : String = "btnCategories";
		
		protected var _type : String;		
		protected var _ui : InteractiveObject;		
		protected var _controls : Controls;		
		
		public function UIComponent($ui : InteractiveObject, $controls : Controls) {
			_ui = $ui;
			_type = $ui.name;
			_controls = $controls;
			init();
		}
		public function apply(...$data) : void {
			switch(_type){
				case UIComponent.TEXTFIELD_ALL:
				case UIComponent.TEXTFIELD_CURRENT:
					_ui["text"] = $data.join(" ");
					break;
				case UIComponent.BUTTON_SLIDESHOW:
					_ui["state"] = $data[0];
					_ui["gotoAndStop"](_ui["state"]);
					break;
			}
		}
		protected function init() : void {
			switch(_type){
				case UIComponent.BUTTON_NEXT:
					_ui.addEventListener(MouseEvent.CLICK, onClickNext, false, 0, true);
					break;
				case UIComponent.BUTTON_PREVIOUS:
					_ui.addEventListener(MouseEvent.CLICK, onClickPrev, false, 0, true);
					break;
				case UIComponent.BUTTON_SLIDESHOW:
					_ui["state"] = false;
					_ui.addEventListener(MouseEvent.CLICK, onClickSlideshow, false, 0, true);
					break;
				case UIComponent.BUTTON_CATEGORIES:
					_ui.addEventListener(MouseEvent.CLICK, onClickCategories, false, 0, true);
					break;
			}
		}
		

		public static function getList() : Object {
			var list: Object = {};
			list[UIComponent.BUTTON_CATEGORIES] = "notRegistered";
			list[UIComponent.BUTTON_NEXT] = "notRegistered";
			list[UIComponent.BUTTON_PREVIOUS] = "notRegistered";
			list[UIComponent.BUTTON_SLIDESHOW] = "notRegistered";
			list[UIComponent.TEXTFIELD_ALL] = "notRegistered";
			list[UIComponent.TEXTFIELD_CURRENT] = "notRegistered";
			return list;
		}
		/**
		 * Events
		 */
		private function onClickCategories(e : MouseEvent) : void {
			_controls.showMenu();
		}
		private function onClickNext(e : MouseEvent) : void {
			_controls.next();
		}
		private function onClickPrev(e : MouseEvent) : void {
			_controls.previous();
		}	
		private function onClickSlideshow(e : MouseEvent) : void {
			_ui["state"] = !_ui["state"];
			_ui["gotoAndStop"](_ui["state"]);
			_controls.toggleSlideshow();
		}

		/**
		 * Getters
		 */
		public function get type() : String {
			return _type;
		}
	}
}
